import axios from 'axios'
import { Code } from './operaCode'
import router from '../../router'
import store from '../../store'
let http = axios.create({
  baseURL: window.api_host,
  // timeout: 200,
  headers: {
    post: {
      'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
    }
  }
})
http.interceptors.request.use(
  config => {
    if (config.url === '/login') {
      return config
    }
    let token = JSON.parse(sessionStorage.getItem('user')).token
    if (token) {
      config.headers['Authorization'] = token
    }
    return config
  },
  () => {
    return Promise.reject({
      code: Code.Error,
      msg: '操作失败！'
    })
  }
)

http.interceptors.response.use(
  res => {
    return res
  },
  err => {
    if (err.code === -101 || err.code === -102) {
      err.code = Code.Overdue
      err.msg = '用户信息已过期，请重新登录！'
    }
    if (err.message === 'Network Error') {
      return Promise.reject({
        code: Code.Offline,
        msg: '网络连接超时，请稍后重试！'
      })
    } else {
      return Promise.reject({
        code: Code.Error,
        msg: '系统错误，请稍后重试！'
      })
    }
  }
)
function successHandle(response) {
  if (response.data && response.data.code === 1) {
    response.errCode = 1
    return response.data
  } else {
    return Promise.reject(response.data)
  }
}
function errorHandle(err) {
  err = Object.assign(
    {
      code: Code.Error,
      msg: '操作失败！'
    },
    err
  )
  err.errCode = err.code
  if (err.code === -101) {
    err.code = Code.Overdue
    err.msg = '用户信息已过期，请重新登录！'
    store
      .dispatch('logout')
      .then(() => {
        this.$msgbox({
          title: '信息过期',
          message: '<span>用户信息已过期，请重新登录！</span>',
          center: true,
          roundButton: true,
          customClass: 'messageBox',
          type: 'info',
          dangerouslyUseHTMLString: true,
          callback: () => {}
        })
        router.push({ name: 'login' })
      })
      .catch(() => {
        router.push({ name: 'login' })
      })
  } else if (err.code === -102) {
    err.msg = '无效token'
    store
      .dispatch('logout')
      .then(() => {
        this.$msgbox({
          title: '无效token',
          message: '<span>无效token！</span>',
          center: true,
          roundButton: true,
          customClass: 'messageBox',
          type: 'info',
          dangerouslyUseHTMLString: true,
          callback: () => {}
        })
        router.push({ name: 'login' })
      })
      .catch(() => {
        router.push({ name: 'login' })
      })
  } else if (err.code === -104 || err.code === -106) {
    err.code = Code.NoAuth
    err.msg = '你无权限进行该操作，请切换用户后重试！'
  } else if (err.code === Code.Offline) {
    err.code = Code.Offline
  } else {
    err.code = Code.Error
  }
  return Promise.reject(err)
}
function sendRequest(method, url, params, type) {
  let obj = {
    url: url,
    method: method,
    params: params
  }
  switch (type) {
    case 'source':
      obj.baseURL = window.api_source
      break
    case 'fhzCount':
      obj.baseURL = window.api_fhzCount
      break
    default:
      obj.baseURL = window.api_host
      break
  }
  var response = http(obj)
  return response.then(successHandle).catch(errorHandle)
}
export default {
  get1(url, params, type) {
    return sendRequest('get', url, params, type)
  },
  post1(url, params, type) {
    return sendRequest('post', url, params, type)
  }
}



// WEBPACK FOOTER //
// ./src/assets/js/requset1.js